import { cloneDeep } from 'lodash-es'
import { http } from '@/utils/http'
import { ElMessage } from 'element-plus'
import { getPlatformUser, getMoneyLimit } from '@/api/user'
import { checkRoles } from '@/stores/roles'
import { initiaStore } from '@/stores/initialization.js'
import _ from 'lodash-es'



export const preferenceStore = () => {
    const userStore = useUserStore()
    const router = useRouter()
    const route = useRoute();
    const initiaStores = initiaStore()
    const state = reactive({
        menuList: [
            {
                value: 'person',
                label: '个人设置'
            }, {
                value: 'overall',
                label: '全局设置'
            }, {
                value: 'environment',
                label: '环境模板'
            },
        ],
        loading: false,
        activeMenu: 'environment',
        formData: {},
        map: {},
        formData_copy: {}

    })

    const getUserAgent = () => {
        let body = {
            params: {
                version: '',
                system: state.formData.base.operate,
                browser: state.formData.base.browser.type
            }
        }

        if (!state.formData.base.userAgent.inherit.includes('all')) {
            body.params.version = state.formData.base.userAgent.inherit.join(',')
        } else {
            delete body.params.version
        }


        http.get(`/environment/get_user_agent`, body).then(res => {
            state.formData.base.userAgent.user_agent = res.data.user_agent
        })
    }

    const getPhone = (value) => {
        state.formData.base.operate = value
        if (value == 'Android' || value == 'iOS') {
            http.get(`/environment/get_phone?system=${state.formData.base.operate}`).then(res => {
                state.map.modelOptions = res.data.list.map(item => {
                    return {
                        value: item.phone_type,
                        label: item.phone_type
                    }
                })

                state.formData.base.model = state.map.modelOptions[0].value
            })
        }

        getUserAgent()

    }

    const changeInherit = () => {
        if (state.formData.base.userAgent.inherit.includes('all') && state.formData.base.userAgent.inherit.length > 1) {
            state.formData.base.userAgent.inherit = state.formData.base.userAgent.inherit.filter(item => item !== 'all');
        }
        getUserAgent()
    }

    // 获取语言
    const getLanguages = () => {
        http.get('/environment/get_languages').then(res => {
            state.map.languages = res.data.list.map(item => {
                return {
                    value: item.language_code,
                    label: item.language_name
                }
            })
        })
    }

    // 获取字体
    const getFonts = () => {
        http.get('/environment/get_fonts').then(res => {
            state.map.fonts = res.data.list.map(item => {
                return {
                    value: item.font,
                    label: item.font
                }
            })
        })
    }

    // 获取时区
    const getTimeZones = () => {
        http.get('/environment/get_time_zones').then(res => {
            state.map.timeZones = res.data.list.map(item => {
                return {
                    value: item.id,
                    label: `${item.code} ${item.timezone}`
                }
            })
        })
    }

    // 获取国家
    const getCountries = () => {
        http.get('/environment/get_countries').then(res => {
            state.map.countryOptions = res.data.list.map(item => {
                return {
                    value: item.country_code,
                    label: item.country_name
                }
            })
        })
    }

    // 获取UA版本
    const getUaVersion = () => {
        http.get(`/environment/get_ua_version?browser=ChroBrowser`).then(res => {
            state.map.inheritOptions = res.data.version.map(item => {
                return {
                    value: item,
                    label: item
                }
            })
        })
    }



    onMounted(() => {
        state.map = initiaStores.initiaData.map
        state.formData_copy = initiaStores.initiaData.formData_copy 
        state.loading = true
        http.get('/environment/get_environment_model').then((res) => {
            state.loading = false
            state.formData = res.data
            getLanguages()
            getFonts()
            getTimeZones()
            getCountries()
            getUaVersion()

            getUserAgent()
        })

        console.log(state.formData_copy);
        


    })

    onUnmounted(() => {

    })

    return {
        state,

        getUserAgent,
        getPhone,
        changeInherit,
        getPhone,
        changeInherit,
        getUserAgent


    }
}
